package com.itfreer.ui.form;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itfreer.ui.R;
import com.itfreer.ui.base.api.IDataView;
import com.itfreer.ui.base.api.IEditTextValidator;
import com.itfreer.ui.base.api.IPower;
import com.itfreer.ui.base.api.OnValueChange;
import com.itfreer.utils.PixelUtils;

/**
 * 定义字典多选择控件
 */
public class DomainCheck extends LinearLayout implements IDataView, IPower, IEditTextValidator {

    private String pid;
    private String nextUI;
    private String fieldName;
    private String dicName = null;
    private boolean readonly = false;
    private boolean required = false;
    private String textGravity = "";
    private String validatorName;
    private String validateTitle = "";
    private IDataView pDataView;
    private Integer loadlevel;

    private TextView title;
    private TextView reqTextView;

    private com.itfreer.ui.base.domain.DomainCheckView radioView;

    public DomainCheck(Context context) {
        super(context);
        setView(context);
    }

    public DomainCheck(Context context, AttributeSet attrs){
        super(context, attrs);
        getAttrs(context, attrs);
        setView(context);
    }

    public DomainCheck(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getAttrs(context, attrs);
        setView(context);
    }

    protected void getAttrs(Context context, AttributeSet attrs){
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SimpleView);
        this.fieldName = a.getString(R.styleable.SimpleView_fieldName);
        this.pid = a.getString(R.styleable.SimpleView_pid);
        this.nextUI = a.getString(R.styleable.SimpleView_nextUI);
        this.dicName = a.getString(R.styleable.SimpleView_dicName);
        this.readonly = a.getBoolean(R.styleable.SimpleView_readonly, false);
        this.textGravity = a.getString(R.styleable.SimpleView_textGravity);
        validatorName = a.getString(R.styleable.SimpleView_validatorName);
        this.validateTitle = a.getString(R.styleable.SimpleView_validateTitle);
        this.required = a.getBoolean(R.styleable.SimpleView_required, false);
        this.loadlevel = a.getInteger(R.styleable.SimpleView_loadLevel, 0);
    }

    /**
     * 设置当前的视图
     * @param context
     */
    protected void setView(Context context){
        this.setBackgroundColor(Color.WHITE);
        this.setGravity(Gravity.CENTER_VERTICAL);
        this.setOrientation(HORIZONTAL);
        this.setPadding(0, PixelUtils.dpToPx(context, 8), 0, PixelUtils.dpToPx(context, 8));

        title = new TextView(context);
        LayoutParams titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        titleParams.setMarginStart(PixelUtils.dpToPx(context, 18));
        title.setLayoutParams(titleParams);
        title.setText(this.validateTitle);
        title.setTextColor(Color.BLACK);
        title.setTextSize(PixelUtils.dpToPx(getContext(), 6));
        this.addView(title);

        reqTextView = new TextView(context);
        LayoutParams reqParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        reqTextView.setLayoutParams(reqParams);
        reqTextView.setText("*");
        reqTextView.setTextColor(Color.parseColor("#FF0000"));
        this.addView(reqTextView);
        if(!required) {
            reqTextView.setVisibility(GONE);
        }

        radioView = new com.itfreer.ui.base.domain.DomainCheckView(context);
        LayoutParams radioParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        radioParams.weight = 1;
        radioView.setLayoutParams(radioParams);
        radioView.setOrientation(HORIZONTAL);
        radioView.setDicName(dicName);
        radioView.setFieldName(fieldName);
        radioView.setPid(pid);
        radioView.setTextGravity(textGravity);
        radioView.setReadonly(readonly);
        radioView.setLoadlevel(loadlevel);
        this.addView(radioView);

        ImageView imageView = new ImageView(context);
        LayoutParams imageParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        imageView.setLayoutParams(imageParams);
        imageView.setImageResource(R.drawable.ic_right_arrow);
        this.addView(imageView);
    }

    @Override
    public void setDicName(String dicName) {
        radioView.setDicName(dicName);
    }

    @Override
    public String getDicName() {
        return radioView.getDicName();
    }

    @Override
    public void setBindData(Object data) {
        radioView.setBindData(data);
    }

    @Override
    public Object getBindData() {
        return radioView.getBindData();
    }

    @Override
    public String getPid() {
        return radioView.getPid();
    }

    @Override
    public void setPid(String pid) {
        radioView.setPid(pid);
    }

    @Override
    public Integer getLoadLevel() {
        return radioView.getLoadLevel();
    }

    @Override
    public String getNextUI() {
        return radioView.getNextUI();
    }

    @Override
    public void setOnValueChange(OnValueChange onValueChange) {
        radioView.setOnValueChange(onValueChange);
    }

    @Override
    public IDataView getNextUIEx() {
        return pDataView;
    }

    @Override
    public void setNextUIEx(IDataView pDataView) {
        this.pDataView = pDataView;
    }

    @Override
    public String getSecurityID() {
        return radioView.getSecurityID();
    }

    @Override
    public void setSecurityVisible(boolean visible) {
        radioView.setSecurityVisible(visible);
    }

    @Override
    public void setFieldName(String name) {
        radioView.setFieldName(name);
    }

    @Override
    public String getFieldName() {
        return radioView.getFieldName();
    }

    @Override
    public void setFieldValue(Object value) {
        radioView.setFieldValue(value);
    }

    @Override
    public Object getFieldValue() {
        return radioView.getFieldValue();
    }

    @Override
    public String getValidatorName() {
        return this.validatorName;
    }

    @Override
    public void setValidatorName(String name) {
        this.validatorName = name;
    }

    @Override
    public String getValidateTitle() {
        return this.validateTitle;
    }

    @Override
    public void setValidateTitle(String validateTitle) {
        this.validateTitle = validateTitle;
        title.setText(this.validateTitle);
    }

    public void setReadonly(boolean readonly) {
        radioView.setReadonly(readonly);
    }

    public void setRequired(boolean required) {
        this.required = required;
        if(this.required) {
            reqTextView.setVisibility(VISIBLE);
        } else {
            reqTextView.setVisibility(GONE);
        }
    }

    public void setTextGravity(String textGravity) {
        this.textGravity = textGravity;
        radioView.setTextGravity(textGravity);
    }

    public void setLoadlevel(Integer loadlevel) {
        radioView.setLoadlevel(loadlevel);
    }
}
